package com.ocean.web.controller.ftp;

import com.ocean.common.annotation.Log;
import com.ocean.common.core.controller.BaseController;
import com.ocean.common.core.domain.AjaxResult;
import com.ocean.common.core.domain.FTPAjaxResult;
import com.ocean.common.core.domain.ZtreeUser;
import com.ocean.common.core.domain.entity.SysBaseUser;
import com.ocean.common.core.domain.entity.SysUser;
import com.ocean.common.core.page.TableDataInfo;
import com.ocean.common.enums.BusinessType;
import com.ocean.common.utils.ShiroUtils;
import com.ocean.system.domain.SysFTPConfigftp;
import com.ocean.system.domain.SysPost;
import com.ocean.system.service.ISysPostService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * FTP信息维护 控制层
 * @author ocean
 */
@Controller
@RequestMapping("/system/post")
public class SysPostController extends BaseController
{
    private String prefix = "system/post";

    @Autowired
    private ISysPostService postService;

    @RequiresPermissions("system:post:view")
    @GetMapping()
    public String operlog()
    {
        return prefix + "/post";
    }

    @RequiresPermissions("system:post:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysFTPConfigftp post)
    {
        startPage();

        List<SysFTPConfigftp> list = null;
        //获取当前登录用户
        SysUser currentUser = ShiroUtils.getSysUser();
        if ("admin,管理员".contains(currentUser.getLoginName())){
            list = postService.selectPostList(post);
        }else{
            Long userId = currentUser.getUserId();
            post.setUserId(userId.toString());
            list = postService.selectPostList(post);
        }
        //List<SysFTPConfigftp> list = postService.selectPostList(post);
        return getDataTable(list);
    }

    /**
     * 选择用户下拉框
     */
    @RequiresPermissions("system:post:list")
    @GetMapping("/selectUserTree")
    public String selectUserTree()
    {
        return prefix + "/userTree";
    }

    /**
     * 加载下拉用户列表树
     */
    @RequiresPermissions("system:post:list")
    @GetMapping("/userTreeData")
    @ResponseBody
    public List<ZtreeUser> userTreeData()
    {
        return postService.userTreeData(new SysBaseUser());
    }

    //确认要停用用户吗
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(@Validated SysFTPConfigftp post)
    {
        SysFTPConfigftp sysFTPConfigftp = postService.selectFTPById(post.getId());
        if(sysFTPConfigftp != null){
            sysFTPConfigftp.setIsOpen(post.getStatus());
            int i = postService.updatePost(sysFTPConfigftp);
            return AjaxResult.success(i);
        }
        return AjaxResult.error("未找到该用户");
    }
    /**
     * 新增FTP信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存FTP信息
     */
    @RequiresPermissions("system:post:add")
    @Log(title = "FTP管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated SysFTPConfigftp post)
    {
        // 校验云平台用户是否绑定过账号
        postService.checkFTPNameUnique(post);
        if(post.getFtpName().equals("")){//如果ftpname为“” 下面几个字段全设置为null
            post.setFtpName("-");
            post.setServerIp("-");
            post.setServerPort("0");
            post.setFtpPassword("-");
        }
        return toAjax(postService.insertPost(post));
    }

    /**
     * 修改FTP信息
     */
    @RequiresPermissions("system:post:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        mmap.put("ftp", postService.selectFTPById(id));
        return prefix + "/edit";
    }

    /**
     * 修改保存FTP信息
     */
    @RequiresPermissions("system:post:edit")
    @Log(title = "FTP管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated SysFTPConfigftp post)
    {
        postService.checkFTPNameUnique(post);
        return toAjax(postService.updatePost(post));
    }

    /**
     * 测试连接
     */
    @RequiresPermissions("system:post:testConnection")
    @PostMapping("/testConnection")
    @ResponseBody
    public FTPAjaxResult testConnection(Long id)
    {
            if(postService.testConnection(id) ==1){
                return FTPAjaxResult.success();
            } else {
                return FTPAjaxResult.error("连接失败，请检查配置是否正确！");
            }
    }

    @RequiresPermissions("system:post:remove")
    @Log(title = "FTP管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        try
        {
            return toAjax(postService.deletePostByIds(ids));
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }
}
